package com.service;

import com.github.wxpay.sdk.WXPayUtil;
import com.util.HttpClient;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @Author zhangqing
 * @Date 2019/6/26 9:36
 **/
@Component
public class WechatService {

    private String appid = "wx8397f8696b538317";
    private String partner = "1473426802";
    private String partnerkey = "T6m9iK73b0kn9g5v426MKfHQH7X8rKwb";
    private String notifyurl = "http://a31ef7db.ngrok.io/WeChatPay/WeChatPayNotify";
       public   Map createNative(String no,String price){
           Map map = new HashMap();
           map.put("appid", appid);
           map.put("mch_id",partner);
           map.put("nonce_str", WXPayUtil.generateNonceStr());
           map.put("body","shoping124df");
           map.put("out_trade_no", no);
           map.put("total_fee",price);
           map.put("spbill_create_ip", "127.0.0.1");
           map.put("notify_url", "http://www.itcast.cn");
           map.put("trade_type", "NATIVE");
           System.out.println("参数："+map);
           try {
               String paramXML = WXPayUtil.generateSignedXml(map, partnerkey);
               HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
               httpClient.setHttps(true);
               httpClient.setXmlParam(paramXML);
               httpClient.post();
               String xmlResult = httpClient.getContent();
               Map<String,String> result = WXPayUtil.xmlToMap(xmlResult);
               System.out.println("result"+result);
               Map map2 = new HashMap();
               map2.put("code_url",result.get("code_url"));
               map2.put("out_trade_no", no);
               map2.put("price",price);
               System.out.println("二维码支付"+map2);
               return  map2;
           } catch (Exception e) {
               e.printStackTrace();
               return new HashMap();
           }

       }
    public Map queryPayStatus(String no){
           Map map  = new HashMap();
           map.put("appid", appid);
           map.put("mch_id", partner);
           map.put("out_trade_no",no);
           map.put("nonce_str",WXPayUtil.generateNonceStr());
        try {
            String paramXML = WXPayUtil.generateSignedXml(map, partnerkey);
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            httpClient.setHttps(true);
            httpClient.setXmlParam(paramXML);
            httpClient.post();
            String content  = httpClient.getContent();
            Map map2 = WXPayUtil.xmlToMap(content);
            System.out.println("查询订单"+map2);
            return map2;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 关闭支付
     * @param no
     * @return
     */
    public Map closePay(String no){
           Map map = new HashMap();
           map.put("appid", appid);
           map.put("mch_id", partner);
           map.put("out_trade_no", no);
           map.put("nonce_str",WXPayUtil.generateNonceStr());

        try {
            String paramXML = WXPayUtil.generateSignedXml(map, partnerkey);
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/closeorder");
            httpClient.setHttps(true);
            httpClient.setXmlParam(paramXML);
            httpClient.post();
            String content = httpClient.getContent();
            Map map2 = WXPayUtil.xmlToMap(content);
            System.out.println("关闭订单"+map2);
            return map2;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 退款
     * @param no
     * @return
     */
    public Map refundPay(String no,String price){
        Map map   = new HashMap();
        map.put("appid", appid);
        map.put("mch_id",partner);
        map.put("out_trande_no",no);
        map.put("nonce_str",WXPayUtil.generateNonceStr());
        map.put("total_fee","1");
        map.put("refund_fee","1");
        map.put("out_refund_no","ghjkgh67897");
        try {
            String paramXML = WXPayUtil.generateSignedXml(map,partnerkey);
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/secapi/pay/refund");

            httpClient.setXmlParam(paramXML);
            httpClient.setHttps(true);
            httpClient.post();
            String content = httpClient.getContent();
            Map map2 = WXPayUtil.xmlToMap(content);
            System.out.println("退款"+map2);
            return map2;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }




    }
    /**
     * 退款查询
     */
    public Map refundQuery(String no){
        Map map = new HashMap();
        map.put("out_trade_no", no);
        map.put("appid",appid);
        map.put("mch_id",partner);
        map.put("nonce_str",WXPayUtil.generateNonceStr());
        try {
          String paramXML = WXPayUtil.generateSignedXml(map, partnerkey);
          HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/refundquery");
          httpClient.setHttps(true);
          httpClient.setXmlParam(paramXML);
          httpClient.post();
          String content = httpClient.getContent();
          Map map2  = WXPayUtil.xmlToMap(content);
            System.out.println("退款查询:"+map2);
            return map2;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }}

    /**
     * 下载对帐单
     * @return
     */
    public Map checkList() throws Exception{
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            String date = simpleDateFormat.format(new Date());

            Map map = new HashMap();
            map.put("appid", appid);
            map.put("mch_id", partner);

            map.put("nonce_str",WXPayUtil.generateNonceStr());

            map.put("bill_date",date);

        try {

            String paramXML = WXPayUtil.generateSignedXml(map, partnerkey);
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/downloadbill");

            httpClient.setXmlParam(paramXML);
            httpClient.setHttps(true);
            httpClient.post();
            String content = httpClient.getContent();
            Map map2 = WXPayUtil.xmlToMap(content);
            System.out.println("对帐单:"+map2);
            return map2;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public static void main(String[] args) throws  Exception {
           WechatService p = new WechatService();
        Random random = new Random();
        System.out.println(random.toString());
      //  String no = "34567ertyfgh6712eas456789sdfg";
       String no  = "45678rtgyhj678fghasdfd2asdfd";
           String price = "1";
           p.createNative(no,price);
        p.queryPayStatus(no);
        //p.closePay(no);
       p.refundPay(no, price);
      //  p.refundQuery(no);
        p.checkList();

    }
}
